//
//  LSNewFeatureView.m
//  KuaiYi
//
//  Created by 黄金林 on 2016/11/14.
//  Copyright © 2016年 HeiMa. All rights reserved.
//

#import "LSNewFeatureView.h"
#import <Masonry.h>
@interface LSNewFeatureView()<UIScrollViewDelegate>
//创建全局的分页控制器
@property(nonatomic,weak)UIPageControl *pgControl;
//创建全局的scollView
@property(nonatomic,weak)UIScrollView *scrollView;
@end
@implementation LSNewFeatureView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self creatScrollView];
    }
    return self;
}
//重写images的setter方法
-(void)setImages:(NSArray *)images{
    _images = images;
    //准备一个初始frame
    CGRect rect = self.scrollView.bounds;
    
    //取出宽度
    CGFloat width = rect.size.width;
    
    //有多少个数据就循环创建多少个imageView
    for (int i = 0; i < images.count; i++) {
        
        //-----创建imageView
        UIImageView *iv = [[UIImageView alloc] init];
        
        //> 设置frame
        iv.frame = CGRectOffset(rect, i * width , 0);
        
        //> 设置图片
        iv.image = images[i];
        
        //> 添加到scrollView
        [self.scrollView addSubview:iv];
        
    }
    
    //设置scrollView的contentSize 为了在最后一页时还能继续往左滚，所以再加一个页面的滚动宽度
    self.scrollView.contentSize = CGSizeMake((images.count+1) * width, 0);
    
    //设置分页指示器的总页数
    self.pgControl.numberOfPages = images.count;
    
}
/**
 *  当scrollView正在滚动时触发
 */
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //通过此方式可以根据偏移值算出它滚动到第几页了，然后把页码设置给分页指示器(为了让滚动一半以后也能让分页指示器显示下一页，因此我们需要+0.5(相当于四舍五入))
    int page = (scrollView.contentOffset.x / scrollView.bounds.size.width ) + 0.5;
    
    self.pgControl.currentPage = page;
    
    //下面这句是上面代码的缩写
    self.pgControl.hidden = page == self.pgControl.numberOfPages;
}

/**
 *  滚动完成事件
 */
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //获得当前已经滚出去的页数
    int page = scrollView.contentOffset.x / scrollView.bounds.size.width;
    
    //如果是滚出去图片数量页，那么就移除当前的新特性界面
    if (page == self.images.count) {
        
        [self removeFromSuperview];
    }
}

//创建滚动视图
-(void)creatScrollView{
    UIScrollView *slView = [[UIScrollView alloc]initWithFrame:self.bounds];
    //    slView.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:arc4random_uniform(256)/255.0];
    [self addSubview:slView];
    //记录变量
    self.scrollView = slView;
    //开启分页
    slView.pagingEnabled = YES;
    //关闭水平,垂直滚动条
    slView.showsVerticalScrollIndicator = NO;
    slView.showsHorizontalScrollIndicator = NO;
    //禁止弹簧效果
    slView.bounces = NO;
    //设置scollView的代理对象为当前视图
    slView.delegate = self;
    //创建分页控制器
    UIPageControl *pgControl = [[UIPageControl alloc]init];
    //设置当前所在页
    pgControl.currentPage = 0;
    //设置分页控制器默认颜色
    pgControl.pageIndicatorTintColor = [UIColor blueColor];
    //设置分页控制器当前页颜色
    pgControl.currentPageIndicatorTintColor = [UIColor yellowColor];
    //禁止分页控制器被点击
    pgControl.userInteractionEnabled = NO;
    [self addSubview:pgControl];
    //分页控制器自动布局
    [pgControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.equalTo(self).offset(-80);
    }];
    //记录分页控制器
    self.pgControl = pgControl;
}

@end
